package exam.demo1;

public class Test7 {

    // 大数乘法
    public static void main(String[] args) {
        String ss = "215";
        String tt = "84";
        System.out.println(solve(ss, tt));
    }

    public static String solve (String ss, String tt)
    {
        char[] s = new StringBuffer(ss).reverse().toString().toCharArray();
        char[] t = new StringBuffer(tt).reverse().toString().toCharArray();
        int m = s.length, n = t.length;
        int[] tmp = new int[m + n];
        // 1. ⽆进位相乘相加
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                tmp[i + j] += (s[i] - '0') * (t[j] - '0');
            }
        }
        // 2. 处理进位
        StringBuffer ret = new StringBuffer();
        int c = 0;
        for(int x : tmp)
        {
            c += x;
            ret.append((char)(c % 10 + '0'));
            c /= 10;
        }
        while(c != 0)
        {
            ret.append((char)(c % 10 + '0'));
            c /= 10;
        }
        // 3. 处理前导零
        while(ret.length() > 1 && ret.charAt(ret.length() - 1) == '0')
        {
            ret.deleteCharAt(ret.length() - 1);
        }
        return ret.reverse().toString();
    }
}
